﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;
using System.Xml;
using Organizer.Model;
using System.Collections.Generic;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;

namespace Organizer.ViewModel
{
    using System.Net;
    using System.Threading;
    using System.Xml.Linq;
    using System.Xml.Serialization;

    using Microsoft.Phone.Shell;

    public class MainPageViewModel : BaseViewModel
    {
        #region constructors

        public MainPageViewModel()
        {
            this.ToOrganizations = new RelayCommand(() => NavigateToOrganizations());
            this.ToTaskList = new RelayCommand(() => NavigateToTaskList());
            this.ToCategoryList = new RelayCommand(() => NavigateToCategoryList());
            this.ToNoteList = new RelayCommand(() => NavigateToNoteList());
            this.ToSettings = new RelayCommand(() => NavigateToSettings());
            this.PinToStartNotesCommand = new RelayCommand(() => PinToStartNotes());
            this.ToMans = new RelayCommand(() => NavigateToMans());
        }

        #endregion

        public string QuoteText { get; set; }
        public string QuoteAuthor { get; set; }

        public List<Man> Men
        {
            get
            {
                return OrganizerDataContext.Instance.Mans.ToList();
            }
        }

        #region commands

        public ICommand ToOrganizations { get; set; }
        public ICommand ToTaskList { get; set; }
        public ICommand ToNoteList { get; set; }
        public ICommand ToCategoryList { get; set; }
        public ICommand ToSettings { get; set; }
        public ICommand PinToStartNotesCommand { get; set; }
        public ICommand ToMans { get; set; }

        #endregion

        #region methods

        /// <summary>
        /// Navigation to Organizations page
        /// </summary>
        private void NavigateToOrganizations()
        {
            ApplicationController.Default.NavigateTo(ViewType.Organizations);
        }

        /// <summary>
        /// Gets Tasks from context.
        /// </summary>
        public List<Task> Tasks
        {
            get { return OrganizerDataContext.Instance.Tasks.Where(task => task.Id != Guid.Empty).ToList(); }
        }

      
        /// <summary>
        /// Navigation to Organizations page
        /// </summary>
        private void NavigateToMans()
        {
            ApplicationController.Default.NavigateTo(ViewType.Mans);
        }

        /// <summary>
        /// Pin to start Notes page
        /// </summary>
        private void PinToStartNotes()
        {
            ShellTile SecondaryTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri != null && x.NavigationUri.ToString().Contains("/NoteList"));

            StandardTileData tile = new StandardTileData();

            if (SecondaryTile == null)
            {
                ////tile.BackgroundImage = new Uri("/Resources/Images/Notes.jpg", UriKind.Relative);
                tile.Title = Properties.Resource.MainPage_hubNotesTitle;
                tile.BackTitle = Properties.Resource.MainPage_hubNotesMessage;
                ////tile.BackBackgroundImage = new Uri("icons/darthvader.png", UriKind.Relative);
                tile.BackContent = Properties.Resource.MainPage_hubNotesMessage;
                ShellTile.Create(new Uri("/NoteList", UriKind.RelativeOrAbsolute), tile);
            }
        }

        /// <summary>
        /// Navigation to Settings page
        /// </summary>
        private void NavigateToSettings()
        {
            ApplicationController.Default.NavigateTo(ViewType.Settings);
        }

        /// <summary>
        /// Navigation to Tasks page
        /// </summary>
        private void NavigateToTaskList()
        {
            ApplicationController.Default.NavigateTo(ViewType.TaskList);
        }

        /// <summary>
        /// Navigation to CategoryList page
        /// </summary>
        private void NavigateToCategoryList()
        {
            ApplicationController.Default.NavigateTo(ViewType.CategoryList);
        }

        /// <summary>
        /// Navigation to NoteList page
        /// </summary>
        private void NavigateToNoteList()
        {
            ApplicationController.Default.NavigateTo(ViewType.NoteList);
        }

        #endregion
    }
}
